#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


//opt(i, j) = max(opt[i - 1][j], opt[i][j - 1]) + nums[i][j];

enum DIR { UP, LEFT };

int max_sum_dp(vector<vector<int>> &nums, vector<vector<int>> &bk)
{
        int n = nums.size();
        vector<vector<int>> dp(n, vector<int>(n, 0));
        dp[1][1] = nums[1][1];
        for(int i = 2; i < n; i++){
                dp[1][i] = dp[1][i - 1] + nums[1][i];
                dp[i][1] = dp[i - 1][1] + nums[i][1];
                bk[i][1] = UP;
                bk[1][i] = LEFT;
        }

        for(int i = 2; i < n; i++){
                for(int j = 2; j < n; j++){
                        if(dp[i - 1][j] >= dp[i][j - 1]){
                                dp[i][j] = dp[i - 1][j] + nums[i][j];
                                bk[i][j] = UP;
                        }else{
                                dp[i][j] = dp[i][j - 1] + nums[i][j];
                                bk[i][j] = LEFT;
                        }
                }
        }
        return dp.back().back();
}

int max_sum(vector<vector<int>> &nums, int row, int col)
{
        if(row == 1 && col == 1){
                return nums[row][col];
        }

        int up = INT32_MIN;
        int left = INT32_MIN;
        if(row > 1){
                up = max_sum(nums, row - 1, col);
        }
        if(col  > 1){
                left = max_sum(nums, row, col - 1);
        }

        return max(up, left) + nums[row][col];
}

int max_2sum_dp(vector<vector<int>> &nums)
{
        int n = nums.size(), m = nums[0].size();
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(2, -1)));
        dp[0][0][0] = nums[0][0];
        dp[0][0][1] = 0;
        for(int i = 1; i < m; i++){
                dp[0][i][0] += nums[0][i];
                dp[0][i][1] = 0;
        }
        for(int i = 1; i < n; i++){
                dp[i][0][0] += nums[i][0];
                dp[i][0][1] = 0;
        }

        for(int i = 1; i < n; i++){
                for(int j = 1; j < m; j++){
                        
                }
        }
}

void show(vector<vector<int>> &nums, vector<vector<int>> &bk, int i, int j)
{
        if(i == 1 && j == 1){
                //cout<<nums[i][j]<<" ";
                nums[i][j] = 0;
                return;
        }
        if(bk[i][j] == UP){
                show(nums, bk, i - 1, j);
        }else{
                show(nums, bk, i, j - 1);
        }
        //cout<<nums[i][j]<<" ";
        nums[i][j] = 0;
}

int main()
{
        int n;
        cin>>n;
        vector<vector<int>> nums(n + 1, vector<int>(n + 1, 0));
        while(true){
                int row, col, x;
                cin>>row>>col>>x;
                if(row == 0 && col == 0 && x == 0){
                        break;
                }
                nums[row][col] = x;
        }
        vector<vector<int>> back(n + 1, vector<int>(n + 1, -1));
        int sum1 = max_sum_dp(nums, back);
        show(nums, back, n, n);
        int sum2 = max_sum_dp(nums, back);
        cout<<sum2 + sum1<<endl;
        return 0;
}